#include <stdio.h>
#include <termios.h>

/*
 * purpose: ask if user wants another transaction
 * method: set tty into char-by-char mode and no-echo mode
 * returns: 0=>yes, 1=>no
 * better: timeout if user walks away
 */

int get_response(char *);
void set_cr_noecho_mode();
int tty_mode(int how);

#define QUESTION "Do you want another transaction"

int main() {
	int response;
	tty_mode(0);
	set_cr_noecho_mode();
	response = get_response(QUESTION);
	tty_mode(1);
	return response;
}


/*
 * purpose: ask a question and wait for a y/n answer
 * method: use getchar and ignore non y/n answers
 * returns: 0=>yes, 1=>no
 */
int get_response(char * question){
	printf("%s (y/n)?", question);
	while (1) {
		switch(getchar()) {
			case 'y':
			case 'Y': return 0;
			case 'n': 
			case 'N':
			case EOF: return 1;
		}
	}
}

/*
 * purpose: put file descriptor 0 into chr-by-chr model and noecho mode
 * method: use bits in termios
 */
void set_cr_noecho_mode() {
	struct termios ttystate;
	tcgetattr(0, &ttystate);				// read current settings
	ttystate.c_lflag &= ~ICANON;			// no buffering
	ttystate.c_lflag &= ~ECHO;				// no echo either
	ttystate.c_cc[VMIN] = 1;				// get 1 char at a time 
	tcsetattr(0, TCSANOW, &ttystate);		// install settings
}

/*
 * how == 0 => save current mode 
 * how == 1 => restore mode
 */
int tty_mode(int how) {
	static struct termios original_mode;
	if (how == 0) {
		tcgetattr(0, &original_mode);
	} else {
		return tcsetattr(0, TCSANOW, &original_mode);
	}
}
